package com.xiaoxin.controller;


import com.xiaoxin.model.mongo.Movement;
import com.xiaoxin.model.vo.MovementsVo;
import com.xiaoxin.model.vo.PageResult;
import com.xiaoxin.model.vo.VisitorsVo;
import com.xiaoxin.service.CommentService;
import com.xiaoxin.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author xiaoxiaode
 * @date 2021-09-03-19:18
 **/

@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;

    @Autowired
    private CommentService commentService;

    /*
     * 发布动态
     * */
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile imageContent[]) throws IOException {
        movementService.publishMovement(movement, imageContent);
        return ResponseEntity.ok(null);

    }

    /*
     * 查询我的动态
     * */
    @GetMapping("/all")
    public ResponseEntity findMyMovement(
            Long userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.findByUserId(userId, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /*
     * 查询好友动态
     * */
    @GetMapping
    public ResponseEntity findFriendMovement(@RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "8") Integer pagesize) {
        PageResult pageResult = movementService.findFriendMovement(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }


    /*
     * 查询推荐动态
     * */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "8") Integer pagesize) {
        PageResult pageResult = movementService.recommendMovement(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }


    /*
     * 查看单条动态
     * */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId) {
        MovementsVo vo = movementService.findById(movementId);
        return ResponseEntity.ok(vo);
    }

    /*
     * 点赞
     * */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.like(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /*
     * 取消点赞
     * */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.dislike(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /*
     * 喜欢
     * */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.love(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /*
     * 取消喜欢
     * */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId) {
        Integer likeCount = commentService.unlove(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /*
     * 访客记录
     **/
    @GetMapping("/visitors")
    public ResponseEntity queryVisitorsList(){
        List<VisitorsVo> list = movementService.queryVisitorsList();
        return ResponseEntity.ok(list);
    }
}
